package org.lyimg.com.imgapplication.RetrofitHelper;

import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.lyimg.com.imgapplication.bean.ImgInfo;
import org.lyimg.com.imgapplication.bean.ImgList;
import org.lyimg.com.imgapplication.bean.ImgType;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

/**
 * Created by liuyong on 2019/1/25.
 */

public class ImgLoader extends ObjectLoader {

    private HttpRetrofitService retrofitService;

    public ImgLoader() {
        retrofitService = RetrofitServiceManager.getInstance().create(HttpRetrofitService.class);
    }

    /**
     * 获取类型
     *
     * @return
     */
    public Observable<List<ImgType>> getImgtype() {
        return observe(retrofitService.getImgType().map(new Function<ResponseBody, List<ImgType>>() {
            @Override
            public List<ImgType> apply(ResponseBody responseBody) throws Exception {
                String result = responseBody.string();

                Document doc = Jsoup.parse(result);
                Elements contents = doc.getElementsByClass("cls-sec-cont");
                Elements content = contents.first().getElementsByTag("dd");

                List<ImgType> list_links = new ArrayList<>();
                for (Element links : content) {
                    list_links.addAll(setlinks(links.getElementsByTag("a")));
                }
                return list_links;
            }
        }));
    }

    /**
     * 获取图集列表
     *
     * @return
     */
    public Observable<List<ImgList>> getImglists(String link) {
        Log.i("TAG","link======" + link);
        return observe(retrofitService.getImgAllList(link).map(new Function<ResponseBody, List<ImgList>>() {

            @Override
            public List<ImgList> apply(ResponseBody responseBody) throws Exception {
                String result = responseBody.string();
                Document doc = Jsoup.parse(result);

                Element conten_page = doc.getElementsByClass("clearfix article-page").first();
                String page_a = conten_page.getElementsByTag("a").first().text();
                String[] pages = page_a.split("/");
                int all_page = Integer.parseInt(pages[1]);

                Element conten = doc.getElementById("listcon");
                Elements links = conten.getElementsByTag("li");

                List<ImgList> imgs = new ArrayList<>();
                for (int j = 0; j < links.size(); j++) {
                    ImgList img = new ImgList();
                    Element content = links.get(j);
                    img.link = content.getElementsByTag("a").first().attr("href");
                    Elements element_imgs = content.getElementsByTag("img");
                    if (element_imgs.size() == 0) {
                    } else {
                        img.img = element_imgs.get(0).attr("data-original");
                        img.title = element_imgs.get(0).attr("alt");
                        img.allpage = all_page;
                        imgs.add(img);
                    }
                }
                return imgs;
            }
        }));
    }

    public Observable<List<ImgInfo>> getImgShow(String url) {
        return observe(retrofitService.getImgShowList(url).map(new Function<ResponseBody, List<ImgInfo>>() {
            @Override
            public List<ImgInfo> apply(ResponseBody responseBody) throws Exception {

                String result = responseBody.string();
                Document doc = Jsoup.parse(result);
                Element content = doc.getElementsByClass("tal").first();
                Element img_link = content.getElementsByTag("img").first();
                String img_url = img_link.attr("src");
//                Log.i("TAG", "handleMessage:img_url======= " + img_url);
                Element content_pages = doc.getElementsByClass("clearfix article-page").first();
                Element element_pages = content_pages.getElementsByTag("a").first();
                String page_a = element_pages.text();
                String[] pages = page_a.split("/");
                int all_page = Integer.parseInt(pages[1]);

                List<ImgInfo> list = new ArrayList<>();
                for (int i = 1; i < all_page; i++) {

                    ImgInfo info = new ImgInfo();
                    String page = "01";
                    if (i < 10) page = "0" + i;
                    else page = "" + i;

                    StringBuilder sb = new StringBuilder(img_url);
                    Log.i("TAG", "handleMessage: ---- " + img_url.lastIndexOf("/"));
                    sb.replace(img_url.length()-6,img_url.length()-4,page);
                    info.src = sb.toString();

                    list.add(info);
                }
                return list;
            }
        }));
    }


    private List<ImgType> setlinks(Elements links) {
        List<ImgType> list_links = new ArrayList<>();
        for (Element link : links) {
            ImgType type = new ImgType();
            type.link = link.attr("href");
            type.title = link.text();
            if (link.attr("href").contains("jingpin") || link.attr("href").contains("wenshen") || link.attr("href").contains("mntp")) {

            } else {
                list_links.add(type);
            }
        }
        return list_links;
    }
}
